package com.zhouyy.api.service.impl;

import com.zhouyy.api.dto.FruitDTO;
import com.zhouyy.api.entity.Fruit;
import com.zhouyy.api.service.IFruitService;
import com.zhouyy.api.service.PracticeService;
import com.zhouyy.common.enums.FruitTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;

/**
 * <p> 练习实现类 </p>
 *
 * @author zhouyy
 * @description
 * @date 2024/03/25 19:05
 */
@Service
public class PracticeServiceImpl implements PracticeService {

    @Resource
    private IFruitService fruitService;

    public BigDecimal getTotalPrice(FruitDTO dto) {

        // 1、获取对象
        Map<Integer, Fruit> typeMap = fruitService.getFruitByType(null);
        Fruit apple = typeMap.get(FruitTypeEnum.APPLE.getType());
        Fruit strawberry = typeMap.get(FruitTypeEnum.STRAWBERRY.getType());
        Fruit mango = typeMap.get(FruitTypeEnum.MANGO.getType());

        // 2、判断是否设置了该水果
        checkFruit(apple, dto.getAppleNum(), FruitTypeEnum.APPLE.getDesc());
        checkFruit(strawberry, dto.getStrawberryNum(), FruitTypeEnum.STRAWBERRY.getDesc());
        checkFruit(mango, dto.getMangoNum(), FruitTypeEnum.MANGO.getDesc());

        // 3、计算价格
        return getSinglePrice(apple, dto.getAppleNum()).add(getSinglePrice(strawberry, dto.getStrawberryNum())).add(getSinglePrice(mango, dto.getMangoNum()));
    }

    @Override
    public BigDecimal getTotalPriceByDiscount(FruitDTO dto) {
        // 1、获取对象
        Map<Integer, Fruit> typeMap = fruitService.getFruitByType(null);
        Fruit apple = typeMap.get(FruitTypeEnum.APPLE.getType());
        Fruit strawberry = typeMap.get(FruitTypeEnum.STRAWBERRY.getType());
        Fruit mango = typeMap.get(FruitTypeEnum.MANGO.getType());

        // 2、判断是否设置了该水果
        checkFruit(apple, dto.getAppleNum(), FruitTypeEnum.APPLE.getDesc());
        checkFruit(strawberry, dto.getStrawberryNum(), FruitTypeEnum.STRAWBERRY.getDesc());
        checkFruit(mango, dto.getMangoNum(), FruitTypeEnum.MANGO.getDesc());


        BigDecimal res = getSinglePrice(apple, dto.getAppleNum());
        res = res.add(getSinglePrice(strawberry, dto.getStrawberryNum()).multiply(BigDecimal.valueOf(0.8)).setScale(2, BigDecimal.ROUND_HALF_UP));
        res = res.add(getSinglePrice(mango, dto.getMangoNum()));
        return res;
    }

    @Override
    public BigDecimal getTotalPriceByFullReduction(FruitDTO dto) {
        BigDecimal discountPrice = getTotalPrice(dto);

        if (discountPrice.compareTo(BigDecimal.valueOf(100)) >= 0) {
            return discountPrice.subtract(BigDecimal.valueOf(10));
        }
        return discountPrice;
    }


    private BigDecimal getSinglePrice(Fruit fruit, Integer num) {
        if (Objects.isNull(num)) {
            return BigDecimal.ZERO;
        }

        return fruit.getPrice().multiply(BigDecimal.valueOf(num));
    }

    private void checkFruit(Fruit fruit, Integer num, String message) {
        if (Objects.isNull(num)) {
            return;
        }

        if (num == 0) {
            return;
        }

        if (Objects.isNull(fruit)) {
            throw new RuntimeException("未设置" + message + "信息！");
        }
    }
}
